<?php

/*

* The price is right (price_game.pro.php)

*/
ob_start();
$rank_check = 1;
include "global.inc.php";

mysql_query("DELETE FROM price_game2 WHERE item = '' AND game = '$game'");

if ($act == "start")
{
	$check = fetch("SELECT * FROM price_game2 WHERE user = '$userid' AND game = '$game'");
	if ($points < 50)
	{
		die(header(error("price_game.php?game=$game","You don't have enough $pointsVar to play.")));
	}
	if ($check[item])
	{
		die(header(error("price_game.php?game=$game","You are already playing a game.")));
	}
	$plays = fetch("SELECT * FROM price_plays2 WHERE user = '$userid' AND date = '$datestamp' AND game = '$game'");
	if (!$plays[id])
	{
		mysql_query("INSERT INTO price_plays2 (user,date,plays,game) VALUES ('$userid','$datestamp','0','$game')");
		$plays = fetch("SELECT * FROM price_plays2 WHERE user = '$userid' AND date = '$datestamp' AND game = '$game'");
	}
	$newplays = $plays[plays] + 1;
	mysql_query("UPDATE price_plays2 SET plays='$newplays' WHERE user = '$userid' AND date = '$datestamp' AND game = '$game'");
	if ($plays[plays] >= 50)
	{
		die(header(error("price_game.php?game=$game","You can only play fifty times a day.")));
	}
	$magic_number = rand(75,124);
	if ($magic_number == 100)
	{
		$magic_number = 125;
	}
	$num_items = mysql_num_rows(mysql_query("SELECT * FROM shop_items2 WHERE game = '$game'"));
	$rand_item = rand(0,$num_items);
	$get_item = fetch("SELECT * FROM shop_items2 WHERE game = '$game' ORDER BY id LIMIT $rand_item,1");
	if ($magic_number < 100)
	{
		$guess = floor(($magic_number / 100) * $get_item[sell_for]);
	}
	if ($magic_number > 100)
	{
		$guess = ceil(($magic_number / 100) * $get_item[sell_for]);
	}
	mysql_query("INSERT INTO price_game2 (user,item,guess,game) VALUES ('$userid','$get_item[item_name]','$guess','$game')");
	points($game,$username,"-50");
	die(header(error("price_game.php?game=$game")));
}
if ($act == "guess")
{
	$fetch = fetch("SELECT * FROM price_game2 WHERE user = '$userid' AND game = '$game'");
	$get_item = fetch("SELECT * FROM shop_items2 WHERE item_name = '$fetch[item]' AND game = '$game'");
	if ($fetch[guess] > $get_item[sell_for])
	{
		$direction = "high";
	}
	if ($fetch[guess] < $get_item[sell_for])
	{
		$direction = "low";
	}
	if ($direction == $guess)
	{
		points($game,$username,100);
		mysql_query("DELETE FROM price_game2 WHERE user = '$userid' AND game = '$game'");
		die(header(error("price_game.php?game=$game","Congratulations, a $fetch[item] costs $get_item[sell_for] $pointsVar.")));
	}
	else
	{
		mysql_query("DELETE FROM price_game2 WHERE user = '$userid' AND game = '$game'");
		die(header(error("price_game.php?game=$game","Sorry, a $fetch[item] costs $get_item[sell_for] $pointsVar.")));
	}
}

?>